//
//  LXWithdrawController.m
//  绿稀全球购
//
//  Created by 王敏 on 16/6/14.
//  Copyright © 2016年 SJT. All rights reserved.
//

#import "LXWithdrawController.h"

#import "LXTextFieldCell.h"

#import "LXOrderSegment.h"

#import "LXWithdrawSuccessController.h"

@interface LXWithdrawController ()<UITableViewDelegate,UITableViewDataSource,LXTextFieldCellDelegate>{
    NSString * _account;
    NSString * _methon;
    NSString * _amount;
}


@property(nonatomic,strong)UITableView * tableView;

@property(nonatomic,strong)LXOrderSegment * segment;

@property(nonatomic,strong)NSArray * placeholders;

@property(nonatomic,strong)NSArray * titles;
@end

@implementation LXWithdrawController

NSString * const withdrawCellID = @"withdrawCell";
NSString * const arrivaltimeCellID = @"arrivaltimeCell";
NSString * const LXTextFieldCellID = @"LXTextFieldCell";



- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = RGBColor(234, 234, 234);
    [self setUpSegment];
    [self setUpTableView];
    [self setUpGesture];
    [self setUpWithdrawButton];
}

#pragma mark - 设置导航栏
-(void)setUpNavi{
    [super setUpNavi];
    self.navigationItem.title = @"佣金提现";
}

#pragma mark - 设置选项卡
-(void)setUpSegment{
    LXOrderSegment * segment = [[LXOrderSegment alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 50)];
    segment.options = @[@"提现到支付宝",@"提现到储蓄卡"];
    segment.selectedIndex = 0 ;
    segment.handle = ^(UIButton* button){
        if(button.tag == 0){
            self.titles = @[@[@"支付宝账号"],@[@"提取金额"]];
            self.placeholders = @[@[@"请输入账号",@"请输入姓名"],@[@"可提取金额"]];
        }
        else{
            self.titles = @[@[@"发卡银行",@"银行储蓄卡",@"开户人姓名"],@[@"提取金额"]];
            self.placeholders = @[@[@"无需选择自动识别",@"请输入储蓄卡号",@"请输入姓名"],@[@"可提取金额"]];
        }
        [self.tableView reloadData];
    };
    segment.enableUnderLine = NO;
    segment.sliderColor = RGBColor(113, 183, 40);
    segment.sliderWidth = ScreenSize.width*0.5;
    segment.sliderHeight = 4;
    segment.selectedColor = RGBColor(113, 183, 40);
    segment.defaultColor = RGBColor(149, 149, 149);
    segment.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:segment];
    self.segment = segment;
    self.titles = @[@[@"支付宝账号"],@[@"提取金额"]];
    self.placeholders = @[@[@"请输入手机号或邮箱",@"请输入姓名"],@[@"可提取金额"]];
}

#pragma mark - 设置tableView
-(void)setUpTableView{
    UITableView * tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    tableView.height = ScreenSize.height - self.segment.height;
    tableView.y = self.segment.botton;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.sectionFooterHeight = 0;
    tableView.sectionHeaderHeight = globalMargin;
    tableView.contentInset = UIEdgeInsetsMake(-20, 0, 0, 0);
    tableView.backgroundColor = [UIColor clearColor];
    self.tableView = tableView;
    [self.view addSubview:tableView];
    
    
    UIView * footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenSize.width, 30)];
    tableView.tableFooterView = footView;
    UIImageView * image = [[UIImageView alloc] initWithFrame:CGRectMake(10, 5, 20, 20)];
    image.image = [UIImage imageNamed:@"doubt_button"];
    [footView addSubview:image];
    UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(image.right+5, 0, footView.width - image.right, image.height)];
    [footView addSubview:label];
    label.userInteractionEnabled = YES;
    label.textColor = HexColor(@"#AAAAAA");
    label.centerY = footView.height*0.5;
    label.textAlignment = NSTextAlignmentLeft;
    label.text = @"提现规则";
    
    [label addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(withdrawRuleClick:)]];
    
    
    [tableView registerClass:[LXTextFieldCell class] forCellReuseIdentifier:LXTextFieldCellID];
}


#pragma mark - 设置立即提现按钮
-(void)setUpWithdrawButton{
    UIButton * withdrawButton = [[UIButton alloc] initWithFrame:CGRectMake(globalMargin, 0, ScreenWidth-2*globalMargin, 38)];
    [withdrawButton setTitle:@"提现" forState:UIControlStateNormal];
    [withdrawButton setBackgroundColor:HexColor(@"#3CB034")];
    [withdrawButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    withdrawButton.titleLabel.font = [UIFont systemFontOfSize:14];
    
    withdrawButton.y = ScreenHeight - withdrawButton.height - 74;
    [withdrawButton addRadius:5];
    [withdrawButton addTarget:self action:@selector(withdrawButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:withdrawButton];
}

#pragma mark - 立即提现按钮点击事假
-(void)withdrawButtonClick:(UIButton*)button{
    NSLog(@"提现按钮点击");
    LXWithdrawSuccessController * vc = [[LXWithdrawSuccessController alloc] init];
    if(self.segment.selectedIndex == 0){
        vc.withdrawMethon = @"支付宝";
        vc.withdrawAmount = _amount;
        vc.withdrawAccount = _account;
    }else{
        vc.withdrawMethon = @"储蓄卡";
        vc.withdrawAmount = _amount;
        vc.withdrawAccount = _account;
    }
    
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self setUpNavi];
}


-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.titles.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.titles[section] count];
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    
    LXTextFieldCell * cell = [tableView dequeueReusableCellWithIdentifier:LXTextFieldCellID];
    cell.indexPath = indexPath;
    cell.delegate = self;
    cell.title = self.titles[indexPath.section][indexPath.row];
    cell.textField.placeholder = self.placeholders[indexPath.section][indexPath.row];
    
    [self addCellHeight:cell.cellHeight indexPath:indexPath];
    
    cell.backgroundImageView.transform = CGAffineTransformIdentity;
     [cell.backgroundImageView addRadius:0];
    cell.enableUnderLine = NO;
    cell.textField.userInteractionEnabled = YES;
    
    if([self.titles[indexPath.section][indexPath.row] isEqualToString:@"发卡银行"]){
            cell.textField.userInteractionEnabled = NO;

    }
    if([self.titles[indexPath.section] count]==1){
        cell.backgroundImage = [UIImage imageWithColor:[UIColor whiteColor] size:CGSizeMake(ScreenWidth-2*globalMargin, cell.cellHeight)];
        [cell.backgroundImageView addRadius:10];
        }
    else if ([self.titles[indexPath.section] count]==2){
            if(indexPath.row == 0){
                cell.backgroundImage = [UIImage imageNamed:@"brokerage_bg02"];
            }else{
               cell.backgroundImage = [UIImage imageNamed:@"brokerage_bg02"];
                cell.backgroundImageView.transform = CGAffineTransformRotate(cell.backgroundImageView.transform, M_PI);
            }
        }
    else{
            if(indexPath.row == 0){
                cell.backgroundImage = [UIImage imageNamed:@"brokerage_bg02"];
                cell.backgroundImageView.transform = CGAffineTransformRotate(cell.backgroundImageView.transform, M_PI);
                cell.enableUnderLine = YES;
            }else if(indexPath.row == [self.titles[indexPath.section] count]-1 ){
                cell.backgroundImage = [UIImage imageNamed:@"brokerage_bg02"];
                
            }else{
                cell.backgroundImageView.transform = CGAffineTransformIdentity;
                cell.backgroundImage = [UIImage imageWithColor:[UIColor whiteColor] size:CGSizeMake(ScreenWidth-2*globalMargin, cell.cellHeight)];
                cell.enableUnderLine = YES;
            }
        }
    
    return cell;

}


-(UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UITableViewHeaderFooterView * view = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"foot"];
    if(!view){
        view = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:@"foot"];
    }
    view.width =ScreenSize.width;
    view.height = 60;
    view.backgroundColor = RandColor;
    return view;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [self cellHeightForIndexPath:indexPath];
}


-(void)withdrawRuleClick:(UITapGestureRecognizer*)gesture{
    NSLog(@"提现规则点击事件");
}

#pragma mark - 输入框cell代理
-(void)LXTextFieldCell:(LXTextFieldCell *)cell DidEndEditing:(NSString *)text{
    if([cell.title isEqualToString:@"支付宝账号"]){
        _account = text;
    }
    if([cell.title isEqualToString:@"提取金额"]){
        _amount = text;
    }
    if([cell.title isEqualToString:@"银行储蓄卡"]){
        _account = text;
    }
    
}

-(void)setUpGesture{
    UISwipeGestureRecognizer * swipetoleft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeGestureEvents:)];
    UISwipeGestureRecognizer * swipetoright = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeGestureEvents:)];
    swipetoleft.direction = UISwipeGestureRecognizerDirectionLeft;
    swipetoright.direction = UISwipeGestureRecognizerDirectionRight;
    [self.view addGestureRecognizer:swipetoleft];
    [self.view addGestureRecognizer:swipetoright];
}

-(void)handleSwipeGestureEvents:(UISwipeGestureRecognizer*)gesture{
    if(gesture.direction == UISwipeGestureRecognizerDirectionLeft){
        self.segment.selectedIndex = self.segment.selectedIndex + 1;
    }
    if(gesture.direction == UISwipeGestureRecognizerDirectionRight){
        self.segment.selectedIndex = self.segment.selectedIndex - 1;
    }
    [self.tableView reloadData];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


@end
